package com.bjsxt.userscenter.common.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

/**
 * 文件加密 需求:对文件本身加密,(文件内容变大)(下载) 交过费:提供一个程序(解密程序),解密
 * 
 * @author wangshSxt
 *
 */
public class FileEncrypt
{
	/**
	 * 加密文件 步骤:文件复制
	 * 
	 * @param souFie
	 *            原始文件
	 * @param tarFile
	 *            加密文件
	 */
	public void encoding(File souFile, File tarFile)
	{
		System.out.println("================");
		BufferedInputStream bis = null ; 
		BufferedOutputStream bos = null ;
		try
		{
			bis = new BufferedInputStream(new FileInputStream(souFile));
			bos = new BufferedOutputStream(new FileOutputStream(tarFile));

			byte[] b = new byte[100];
			int len = 0;
			Random random = new Random();
			/* 100个字符我只用20,60 
			 * 读取从20,往后数60个字节;实际是从20--80(字节);
			 * */
			while ((len = bis.read(b,20,60)) != -1)
			{
				
				for (int i = 0; i < 20; i++)
				{
					b[i] = Byte.valueOf(random.nextInt(256) - 128 + "") ; 
					
				}
				
				for (int i = 80; i < 100; i++)
				{
					b[i] = Byte.valueOf(random.nextInt(256) - 128 + "") ; 
					
				}
				System.out.println(len + "---->" + Arrays.toString(b) + "=====" + (random.nextInt(256) - 128));
				/*bos.write(b, 0, len);*/
				if(len == 60)
				{
					/* 表示不是最后一次 */
					bos.write(b, 0, 100);
				}else
				{
					/* 最后一次 */
					bos.write(b, 0, len);
				}
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				bis.close();
				bos.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public void decoding(File souFile,File tarFile)
	{
		BufferedInputStream bis = null ; 
		BufferedOutputStream bos = null ;
		try
		{
			bis = new BufferedInputStream(new FileInputStream(souFile));
			bos = new BufferedOutputStream(new FileOutputStream(tarFile));

			byte[] b = new byte[100];
			int len = 0;
			/* 100个字符我只用20,60 */
			while ((len = bis.read(b)) != -1)
			{
				System.out.println(len + "---->" + Arrays.toString(b));
				if(len == 100)
				{
					bos.write(b, 20, 60);
				}else
				{
					bos.write(b, 20, len);
				}
				/*if(len == 60)
				{
					 表示不是最后一次 
					bos.write(b, 0, 100);
				}else
				{
					 最后一次 
					bos.write(b, 0, len);
				}*/
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				bis.close();
				bos.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args)
	{
		File souFile = new File("d:/1.jpg");
		File tarFile = new File("d:/2.jpg");
		File tar2File = new File("d:/3.jpg");
		FileEncrypt fileEncrypt = new FileEncrypt();
		fileEncrypt.encoding(souFile, tarFile);
		
		fileEncrypt.decoding(tarFile, tar2File);
	}
}
